/*
 * =====================================================================================
 *
 *       Filename:  helloworld.c
 *
 *    Description:  第一个linux module编写
 *
 *        Version:  1.0
 *        Created:  2018年08月28日 17时05分35秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  NYB (), niuyabeng@126.com
 *   Organization:  
 *
 * =====================================================================================
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/rcupdate.h>
#include <linux/kthread.h>

static int stall = 30;
module_param(stall, int, 0600);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("NYB");


static int cpu_stall(void *args)
{
    unsigned long stop_at = get_seconds() + stall;
    rcu_read_lock();
    while(ULONG_CMP_LT(get_seconds(), stop_at)) {
        continue;
    }

    rcu_read_unlock();
    return 0;
}


static int cpu_stall_init(void)
{
    int ret;
    struct task_struct *stall_task = kthread_run(cpu_stall, NULL, "cpu_stall");
    if(IS_ERR(stall_task)) {
        ret = PTR_ERR(stall_task);
        stall_task = NULL;
        return ret;
    }
    return 0;
}


static int helloworld_init(void)
{
    printk(KERN_EMERG"%s: start\n", __func__);
    cpu_stall_init();
    return 0; 
}

static void helloworld_exit(void)
{
    printk(KERN_EMERG"%s: start\n", __func__);
}
 
module_init(helloworld_init);
module_exit(helloworld_exit);

